#include "Graphics/OpenGL/GLDeviceIndexBuffer.h"

namespace tyro
{
	namespace Graphics
	{


		


		GLDeviceIndexBuffer::GLDeviceIndexBuffer( IDeviceBufferManagerBase* mgr, VertexIndexType indexType, size_t numberIndecies )
			:	IDeviceIndexBuffer(mgr, indexType, numberIndecies)
		{

			glGenBuffersARB( 1, &bufferId );
			glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferId);
			glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeInBytes, NULL, GL_STATIC_DRAW_ARB);
		}

		void GLDeviceIndexBuffer::WriteData( size_t memOffset, size_t length, const void* source )
		{
			glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferId );

			if ( memOffset == 0 && length == sizeInBytes )
			{
				glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeInBytes, source, GL_STATIC_DRAW_ARB);
			}

		}

		void GLDeviceIndexBuffer::ReadData( size_t memOffset, size_t length, void* destination )
		{

		}

		void* GLDeviceIndexBuffer::LockImpl( size_t offset, size_t length )
		{
			void* retrn = 0;
			GLenum access = 0;
			glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferId );
			glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeInBytes, NULL, GL_STATIC_DRAW_ARB);
			access = GL_WRITE_ONLY_ARB;
			void* pBuffer = glMapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, access );

			retrn = static_cast<void*>( static_cast<unsigned char*>(pBuffer) + offset);
			return retrn;
		}

		void GLDeviceIndexBuffer::UnlockImpl( void )
		{
			glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, bufferId );
			bool s = glUnmapBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB );
		}

	}
}